<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="..././css/public.css" media="all">
    
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button type="button" id="input_form"  class="layui-btn layui-btn-sm layui-btn-disabled" disabled lay-event="input_form"> 导入表格 </button>
                <button class="layui-btn layui-btn-sm layui-btn-disabled"  disabled id="match_QQ" lay-event="match_QQ"> 关联QQ </button>
                <button class="layui-btn layui-btn-sm layui-btn-disabled"  disabled id="submit" lay-event="submit"> 提交数据 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter" ></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/axios.min.js" charset="utf-8"></script>
<script>


    layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            upload = layui.upload;

        var post_flag = true;
        
      

        table.render({
            elem: '#currentTableId',
            data:[],
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {type: "numbers", width: 50},
                {field: 'uid', width: 120, title: '学号', sort: true, edit: "text", templet: function(d){if (!d.uid) {post_flag=false;return '<span style="color: #c00;">缺少学号</span>';}else return d.uid;}},
                {field: 'name', width: 80, title: '姓名', sort: true, edit: "text", templet: function(d){if (!d.name) {post_flag=false;return '<span style="color: #c00;">缺少姓名</span>';}else return d.name;}},
                {field: 'sex', width: 80, title: '性别', sort: true, edit: "text"},
                {field: 'banji', width: 110, title: '班級', sort: true, edit: "text", templet: function(d){if (!d.banji) {post_flag=false;return '<span style="color: #c00;">缺少班级</span>';}else return d.banji;}},
                {field: 'phone', width: 125, title: '手机', sort: true, edit: "text"},
                {field: 'QQ', width: 130, title: 'QQ', sort: true, edit: "text", templet: function(d){if (!d.QQ) {post_flag=false;return '<span style="color: #c00;">缺少QQ号</span>';}else return d.QQ;}},
                {field: 'beizhu', title: '备注', sort: true, edit: "text"},
                {title: '操作', minWidth: 60, toolbar: '#currentTableBar', fixed: "right"}
            ]],
            // limits: [10, 15, 20, 25, 50, 100],
            // limit: 100,
            // page: true,
            skin: 'line'
        });


        /**
         * toolbar监听事件
         */
        $("#input_form").removeClass("layui-btn-disabled");
        $("#input_form").attr("disabled",false);

        function toobar_btn_change(){
            $("#input_form").addClass("layui-btn-disabled");
            $("#input_form").attr("disabled",true);

            $("#match_QQ").removeClass("layui-btn-disabled");
            $("#match_QQ").attr("disabled",false);

            $("#submit").removeClass("layui-btn-disabled");
            $("#submit").attr("disabled",false);
        }
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                // layer.alert(JSON.stringify(data));
                // console.log(data,data.length);
                if (data.length==0) {layer.msg("请选中一个元素进行删除");return false;}
                layer.confirm("确定要删除？",function(index){
                    console.log(obj);
                    console.log(data);
                    data0 = []
                    for(var i=0; i< data.length; ++i){
                        data0.push(data[i].uid)
                    }

                    layer.close(index);
                });
            } else if (obj.event == "match_QQ") {
                // console.log("obj",obj);
                console.log(obj.config.data);
                var no_match = [];
                var data = obj.config.data
                for (var i=0; i<data.length; ++i){
                    if (!data[i].QQ){
                        no_match.push({uid:data[i].uid, name:data[i].name, banji: data[i].banji});
                    }
                }
                console.log("no_match ", no_match);
                axios.post('/math_QQ', no_match)
                    .then(function (res) {
                        post_flag=true;
                        var info_map = res.data.data;
                        // console.log("info_map", info_map);
                        var info;
                        var oldData = layui.table.cache["currentTableId"]; 
                        // console.log("oldData", oldData);
                        for (var i=0; i<oldData.length; ++i){
                            info = info_map[oldData[i].name]
                            if (info && oldData[i].uid == info[0]) {
                                oldData[i].QQ = info[1];
                            }
                        }
                        // console.log("oldData2", oldData);
                        table.reload('currentTableId', {
                            data: oldData
                        });
                        console.log("post_flag", post_flag);

                        toobar_btn_change();

                    }).catch(function (error) {
                        layer.msg("QQ查询失败!",{icon:2});
                    }); 
            } else if (obj.event == "submit"){
                if (post_flag) {
                    var table_data = layui.table.cache["currentTableId"];
                    axios.post('/daoru_submit', table_data)
                        .then(function (res) {
                            var data = res.data;
                            console.log(res);
                            if (data.code ==0 ) {
                                parent.layer.msg("数据提交成功!\n共修改"+data.count+"项",{icon:1});
                                parent.layui.table.reload("studentTableId");
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            } else {
                                layer.msg("数据提交失败!\nmsg: " +data.msg,{icon:2});
                            }
                        }).catch(function (error) {
                            console.log(error)
                            layer.msg("数据提交失败!",{icon:2});
                        }); 
                } else {
                    layer.msg("请补全数据后再提交");
                }


            }
        });
        

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                obj.del();
            }
        });

          // 上传文件
        upload.render({
            elem: "#input_form",
            url: "/loadDataFromFile",
            acceptMime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            accept: "file",
            exts: "xlsx",
            done: function(res) {
                console.log(res);
                table.reload('currentTableId', {
                    data: res.data
            });

            
            toobar_btn_change();



            },
            error: function(err){
                console.log("error",err);
            }
        })

    });
</script>

</body>
</html>